package wp.ggt.com.wpview.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import wp.ggt.com.wpview.R;

/**
 * Created by KF on 2016/3/12.
 */
public class SwitchView extends View {

    private Paint paint;
    private Bitmap close_back;
    private Bitmap close_off;
    private Bitmap open_back;
    private Bitmap open_on;
    private boolean flag_close_open = false;
    private float downX;
    private float moveX;
    private boolean flag_ismove = false;
    private boolean flag_moveStats = false;
    private SwitchChangeListerer switchChangeListerer;
    private int i;

    public void setSwitchChangeListerer(SwitchChangeListerer switchChangeListerer) {
        this.switchChangeListerer = switchChangeListerer;
    }

    public interface SwitchChangeListerer {
        void switchChangeListener(boolean openOrclose);
    }

    public SwitchView(Context context) {
        super(context);
    }

    public SwitchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }


    public SwitchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int modeWith = MeasureSpec.getMode(widthMeasureSpec);
        int sizeWith = MeasureSpec.getSize(widthMeasureSpec);
        switch (modeWith) {
            case MeasureSpec.EXACTLY:
                Matrix matrix = new Matrix();
                matrix.postScale(((float) sizeWith / (float) open_back.getWidth()), (float) sizeWith / (float) open_back.getWidth()); //长和宽放大缩小的比例
                open_back = Bitmap.createBitmap(open_back, 0, 0, open_back.getWidth(), open_back.getHeight(), matrix, true);
                close_back = Bitmap.createBitmap(close_back, 0, 0, close_back.getWidth(), close_back.getHeight(), matrix, true);
                close_off = Bitmap.createBitmap(close_off, 0, 0, close_off.getWidth(), close_off.getHeight(), matrix, true);
                open_on = Bitmap.createBitmap(open_on, 0, 0, open_on.getWidth(), open_on.getHeight(), matrix, true);
        }
        setMeasuredDimension(open_back.getWidth(), open_back.getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        i = (open_back.getHeight() - open_on.getHeight()) / 2;
        if (!flag_ismove) {
            flag_moveStats = false;
            if (flag_close_open) {
                canvas.drawBitmap(open_back, 0, 0, paint);
                canvas.drawBitmap(close_off, open_back.getWidth() - i - close_off.getWidth(), i, paint);
            } else {
                canvas.drawBitmap(close_back, 0, 0, paint);
                canvas.drawBitmap(open_on, i, i, paint);
            }
        } else {
            if (!flag_close_open) {
                if (moveX > (open_back.getWidth() - open_on.getWidth() - i) / 2) {
                    canvas.drawBitmap(open_back, 0, 0, paint);
                    canvas.drawBitmap(close_off, moveX, i, paint);
                } else {
                    canvas.drawBitmap(close_back, 0, 0, paint);
                    canvas.drawBitmap(open_on, moveX, i, paint);
                }
            } else {
                if (moveX < -(open_back.getWidth() - open_on.getWidth() - i) / 2) {
                    canvas.drawBitmap(close_back, 0, 0, paint);
                    canvas.drawBitmap(open_on, open_back.getWidth() - i - close_off.getWidth() + moveX, i, paint);
                } else {
                    canvas.drawBitmap(open_back, 0, 0, paint);
                    canvas.drawBitmap(close_off, open_back.getWidth() - i - close_off.getWidth() + moveX, i, paint);
                }
            }
        }
        canvas.restore();
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
    }

    public void setStatus(boolean flag_close_open) {
        this.flag_close_open = flag_close_open;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getRawX();
                break;
            case MotionEvent.ACTION_MOVE:
                moveX = event.getRawX() - downX;
                if (flag_close_open) {
                    if (moveX < open_on.getWidth() - open_back.getWidth()+2*i) {
                        moveX = open_on.getWidth() - open_back.getWidth()+2*i;
                    }
                    if (moveX > 0) {
                        moveX = 0;
                    }
                } else {
                    if (moveX > open_back.getWidth() - open_on.getWidth() - i) {
                        moveX = open_back.getWidth() - open_on.getWidth() - i;
                    }
                    if (moveX < i) {
                        moveX = i;
                    }
                }
                flag_ismove = true;
                flag_moveStats = true;
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                flag_ismove = false;
                if (flag_moveStats) {
                    if (flag_close_open) {
                        flag_close_open = moveX >= -(open_back.getWidth() - open_on.getWidth() - i) / 2;
                    } else {
                        flag_close_open = moveX > (open_back.getWidth() - open_on.getWidth() - i) / 2;
                    }
                    if (switchChangeListerer != null)
                        switchChangeListerer.switchChangeListener(flag_close_open);
                    invalidate();
                }
                break;
        }
        return flag_moveStats || super.onTouchEvent(event);
    }


    private void initView() {
        paint = new Paint();
        close_back = BitmapFactory.decodeResource(getResources(), R.drawable.close_back);
        close_off = BitmapFactory.decodeResource(getResources(), R.drawable.close_off);
        open_back = BitmapFactory.decodeResource(getResources(), R.drawable.open_back);
        open_on = BitmapFactory.decodeResource(getResources(), R.drawable.open_on);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                flag_close_open = !flag_close_open;
                if (switchChangeListerer != null)
                    switchChangeListerer.switchChangeListener(flag_close_open);
                invalidate();
            }
        });
    }
}
